﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using moose.irc;

namespace mooseirc.client.windowsforms {

    /// <summary>
    /// Form used for messaging a user directly
    /// </summary>
    [CLSCompliant(true)]
    public class QueryForm : CommunicationChannelForm {


        #region [Local Attributes]

        /// <summary>
        /// Session we are communicating over
        /// </summary>
        IrcSession m_session;

        /// <summary>
        /// Reference to the user we're communicating with
        /// </summary>
        IrcUser m_user;

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Query Form
        /// </summary>
        /// <param name="session">The IRC Session</param>
        /// <param name="parent_form">Parent MDI Form</param>
        /// <param name="user">User we are communicating with</param>
        public QueryForm(IrcSession session, MainForm parent_form, IrcUser user)
            : base(parent_form, new controls.QueryControl()) {
            m_session = session;
            m_user = user;

            input = ((controls.QueryControl)m_HostedControl).input;
            output = ((controls.QueryControl)m_HostedControl).output;

            Text = m_user.Name;

            input.UserResponse +=new ResponseHandler(input_UserResponse);

            return;
        }


        #endregion


        #region [Event Handlers]

        /// <summary>
        /// Fires when user enters some input
        /// </summary>
        /// <param name="response">user input</param>
        void input_UserResponse(string response) {

            if (response.Length > 1) {
                if (response[0] == '/') {
                    m_session.send(response.Substring(1).Trim());
                } else {
                    privmsg(response);
                }
            } else {
                privmsg(response);
            }

        }

        #endregion

        #region [Helper Methods]

        /// <summary>
        /// Send a private message to the channel
        /// </summary>
        /// <param name="message">message to send to the channel</param>
        private void privmsg(string message) {
            m_session.send("PRIVMSG " + m_user.Name + " :" + message);
            return;
        }

        #endregion

    }
}
